#!/bin/bash

# Java 应用停止脚本
APP_NAME="wechat-login"
PID_FILE="/var/run/$APP_NAME.pid"

if [ ! -f "$PID_FILE" ]; then
    echo "$APP_NAME is not running"
    exit 1
fi

PID=$(cat $PID_FILE)

if ps -p $PID > /dev/null; then
    echo "Stopping $APP_NAME (PID: $PID)..."
    kill $PID
    
    # 等待进程结束
    TIMEOUT=30
    while [ $TIMEOUT -gt 0 ] && ps -p $PID > /dev/null; do
        sleep 1
        TIMEOUT=$((TIMEOUT - 1))
    done
    
    if ps -p $PID > /dev/null; then
        echo "Force killing $APP_NAME..."
        kill -9 $PID
    fi
    
    rm -f $PID_FILE
    echo "$APP_NAME stopped"
else
    echo "$APP_NAME is not running (PID: $PID)"
    rm -f $PID_FILE
fi
